package com.frank.libandroid;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.databinding.ViewDataBinding;

import com.frank.libandroid.butterknife.ButterKnife;
import com.lxj.xpopup.core.CenterPopupView;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public abstract class CenterPopup<V extends ViewDataBinding> extends CenterPopupView {
    protected V mBinding;

    public CenterPopup(@NonNull Context context) {
        super(context);
    }

    protected void addInnerContent() {
        Class<Object> generic = (Class<Object>) ReflectUtil.getGeneric(this, 0);
        try {
            Method method = generic.getMethod("inflate", LayoutInflater.class, ViewGroup.class, boolean.class);
            mBinding = (V) method.invoke(null, LayoutInflater.from(this.getContext()), centerPopupContainer, false);
            ViewGroup.LayoutParams params = mBinding.getRoot().getLayoutParams();

            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(params.width, params.height);
            layoutParams.gravity = Gravity.CENTER;
            this.centerPopupContainer.addView(mBinding.getRoot(), layoutParams);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        initView();
        initData();
    }

    protected void initData() {
    }

    protected void initView() {
        ButterKnife.bind(this, mBinding.getRoot());
    }
}
